var points = [];//存放所有点
var pIdSequence = 1;//点序列号
var zonePoints = [];//存放区域核心点
var kCount = 1;//执行优化次数-用于显示
var stepIndex = 1;//执行步骤序号

/**
 * 重置所有内容
 */
function init() {
    points = [];
    zonePoints = [];
    stepIndex = 1;
    $("#canvas").empty();
    $("#cons").html('日志：<br>');
    pIdSequence = 1;
    kCount = 1;
    $(".stepDiv").addClass("hide");
    $("#step1").removeClass("hide");
    $("#step1btn").attr("disabled", "disabled");
}

/**
 * 在坐标系中添加一个点（或一个分组点）
 * @param event 鼠标点击事件
 */
function addPoint(event) {
    //console.table(event)
    if (stepIndex === 1) {
        var p = {'x': event.x - 5, 'y': event.y - 5, 'id': pIdSequence};
        points.push(p);
        var point = $("<span id='p" + pIdSequence + "'><span class='title'>p["+pIdSequence+"]</span></span>");
        point.addClass("point").css({'left': p.x + 'px', 'top': p.y + 'px'});
        $("#canvas").append(point);showTitle();
        printLog('p[' + p.id + ']{x:' + p.x + ',y:' + p.y + '}所属类别:' + (p.tpId === undefined ? '未设定' : p.tpId));
        pIdSequence++;
        if (points.length > 3) {
            $("#step1btn").removeAttr("disabled");
        }
    } else if (stepIndex === 2) {
        var k = parseInt($("#k").val());
        if (k > zonePoints.length) {
            var tpId = zonePoints.length;
            var tp = {'x': event.x - 5, 'y': event.y - 5, 'id': tpId, 'color': randomColor()};
            var zonePoint = $("<span id='tp" + tpId + "'><span class='title'>z"+tpId+"</span></span>");
            zonePoint.addClass("typePont").css({'left': tp.x + 'px', 'top': tp.y + 'px'});
            $("#canvas").append(zonePoint);showTitle();
            printLog('<a style="color: ' + tp.color + '">zonePont[' + tpId + ']{x:' + tp.x + ',y:' + tp.y + '}</a>');
            zonePoints.push(tp);
            if (zonePoints.length === k) {
                $("#step2btn").removeAttr("disabled");
            }
        }

    }
}

/**
 * 执行步骤
 * @param step 下一步序号
 */
function execStep(step) {//执行算法步骤
    $(".stepDiv").addClass("hide");
    if (step === 2) {
        $("#step2").removeClass("hide");
    } else if (step === 3) {
        $("#step3").removeClass("hide");
        $(".kCount").text(kCount);
    } else if (step === 4) {
        $(".showK").text(parseInt($("#k").val()));
        kMeans(points, zonePoints);
        $("#step4").removeClass("hide");
    } else if (step === 5) {
        var finishFlag = calcCenter(points, zonePoints);
        if (!finishFlag) {
            kCount++;
            $(".kCount").text(kCount);
            $("#step3").removeClass("hide");
        } else {
            $("#step5").removeClass("hide");
        }
    }
    stepIndex++;
}

/**
* 显示标题
*/
function showTitle(){
    if($("#showTitle").attr("checked")){
        $(".title").show();
    }else{
        $(".title").hide();
    }
}


/**
 * 生成新分区的颜色值
 */
function randomColor() {
    var r = randomColorVal();
    var g = randomColorVal();
    var b = randomColorVal();
    return '#' + r + g + b;
}

/**
 * 生成新分区的颜色值 0-240之间，2位
 */
function randomColorVal(){
    var val = Math.round(Math.random() * 240).toString(16)+'';
    if(val.length < 2){
        val = '0' + val;
    }
    return val;
}

/**
 * 打印日志
 * @param p
 */
function printLog(info) {
    $("#cons").append('<p>' + info + '</p>');
    $("#cons")[0].scrollTop = $("#cons")[0].scrollHeight;
}